#include "grammar.h"
#include "table.h"
#include "result.h"
#include "global.h"

using namespace fouine;

Grammar::Grammar(const std::vector<Rule>& rules_)
:_rules(rules_),
 _max_length(20)
{
}

Result Grammar::analyze(const String& s) const
{
  Table table(_rules.size(), s.size(), maxLength());
  std::vector<Rule>::const_iterator rule_it;
  for (StrLen l=1; l<=maxLength(); l++)
  {
    for (RuleId r=0 ; r<_rules.size(); r++)
    {
      const Rule& rule = _rules[r];
      for (StrId first=0; first<=table.strLength()-l; first++)
        table(l,r,first) = rule.match(s, l, first, table);
    }
  }
  return Result();
}

Grammar::Grammar()
{}


Grammar::Grammar(const Grammar& other)
:_rules(other._rules),
 _max_length(other._max_length)
{}

int Grammar::maxLength() const {
  return _max_length;
}

void Grammar::setMaxLength(int max_length_) {
  _max_length = max_length_;
}